package cn.edu.dgut.function;
import cn.edu.dgut.Javabean_object.userinfo;

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import java.io.*;

public class readfile {
    private String fileName = "userinfo.txt";

    public userinfo[] getUers() throws IOException {
        int n = 0;
        Reader reader = new FileReader(fileName);
        BufferedReader bufferedReader = new BufferedReader(reader);
        String str1= null;
        while ((str1 = bufferedReader.readLine()) != null) {
            //过滤掉空格行
            if(!str1.trim().equals("")){
                n++;
            }
        }
        bufferedReader.close();
        reader.close();

        int len = 0;
        userinfo[] users;
        if(n==0){
            return null;
        }
        users = new userinfo[n];
        Jsonb jsonb = JsonbBuilder.create();
        Reader reader1= new FileReader(fileName);
        BufferedReader bufferedReader1 = new BufferedReader(reader1);

        String str= null;
        while ((str = bufferedReader1.readLine()) != null) {
            if(!str.trim().equals("")){
                users[len++] = jsonb.fromJson(str,userinfo.class);
            }
        }
        bufferedReader1.close();
        reader1.close();
        return users;
    }


    public userinfo localinfo(String str) throws IOException {
        userinfo [] users = getUers();
        if(users!=null) {
            for (int i = 0; i < users.length; i++) {
                if (users[i].getUser_number().equals(str)) {
                    return users[i];
                }
                if ( users[i].getEmail() != null ) {
                    if(users[i].getEmail().equals(str)){
                        return users[i];
                    }
                }
                if ( users[i].getPhone_number() != null ) {
                    if(users[i].getPhone_number().equals(str)){
                        return users[i];
                    }
                }
            }
        }
        return null;
    }


    public void resetUserInfo(userinfo user) throws IOException {
        userinfo[] users = getUers();
        StringBuffer bu = new StringBuffer();
        BufferedWriter out = null;
        Jsonb jsonb = JsonbBuilder.create();
        File file = new File("userinfo.txt");
        out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,false)));
        for (int i = 0; i < users.length; i++) {
            if(user.getUser_number().equals(users[i].getUser_number())){
                users[i] = user;
            }
            String infoJson = jsonb.toJson(users[i]);
            bu.append(infoJson+"\n");
        }
        String userString = bu.toString();
        out.write(userString);
        out.close();
    }

    public void setUserInfo(String userinfo){
        BufferedWriter out = null;
        File file = new File("userinfo.txt");
        try {
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
            out.write(userinfo+"\n");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
